#include <QDebug>
#include "TreeItem.h"
TreeItem::TreeItem() :m_parentItem(NULL)
{
}

TreeItem::TreeItem(const QList<QVariant> &data, TreeItem* parent) : m_parentItem(NULL)
{
//    m_nRowType = eType;
    m_parentItem = parent;
    m_itemData = data;
}

TreeItem::~TreeItem()
{
    if (0 != m_childItems.size())
        qDebug()<<"delete TreeItem m_childItems.size="<<m_childItems.size();
    qDeleteAll(m_childItems);
}

void TreeItem::appendChild(TreeItem *item)
{
    item->setParent(this);
    m_childItems.append(item);
}

void TreeItem::deleteAllChild()
{
    for (int index = 0; index < m_childItems.size(); index++)
    {
        m_childItems[index]->deleteAllChild();
    }
    qDeleteAll(m_childItems);
    m_childItems.clear();
}

TreeItem *TreeItem::child(int row)
{
    return m_childItems.value(row);
}

int TreeItem::childCount() const
{
    return m_childItems.count();
}

int TreeItem::columnCount() const
{
    return m_itemData.count();
    //return 1;
}

QVariant TreeItem::data(int column) const
{
    return m_itemData .value(column);
}

TreeItem *TreeItem::parent()
{
    return m_parentItem;
}

void TreeItem::setParent(TreeItem *parent)
{
    m_parentItem = parent;
}

void TreeItem::updateChild(int keyCol, TreeItem *child)
{
    for (int index = 0; index < m_childItems.size(); index++)
    {
        if (m_childItems[index]->data(keyCol) == child->data(keyCol))
        {
            for(int i = 0; i < columnCount(); ++i)
            {
                m_childItems[index]->setData(i, child->data(i));
//                m_childItems[index]->setData(i, 0);
            }
//            m_childItems[index]->deleteAllChild();
//            m_childItems.removeAt(index);
//            m_childItems.insert(index,child);
//            m_childItems[index] = child;
            qDebug()<<"update key "<<child->data(keyCol);
            break;
        }
    }
}

void TreeItem::setData(int column, QVariant value)
{
    m_itemData[column] = value;
}

int TreeItem::row() const
{
    if (!m_parentItem) { return 0; }

    return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this));
}
